package com.cskaoyan.service.impl;


import com.cskaoyan.bean.MarketKeyword;
import com.cskaoyan.bean.MarketKeywordExample;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.param.CommonData;

import com.cskaoyan.bean.wubang.bo.KeywordBO;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.service.KeywordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @auther: wubang
 * @description:
 * @date: 2022-06-27 20:11
 */
@Service
@Transactional
public class KeywordServiceImpl implements KeywordService {

    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    @Override
    public CommonData<MarketKeyword> selectKeywordList(BaseParam baseParam, String keyword, String url) {
        // 开启分页
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        // 创建example对象，添加准则
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        if (keyword != null) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (url != null) {
            criteria.andUrlLike("%" + url + "%");
        }
        // 调用mapper查询数据库
        List<MarketKeyword> keywords = marketKeywordMapper.selectByExample(marketKeywordExample);
        // 获取分页信息
        PageInfo<MarketKeyword> pageInfo = new PageInfo<>(keywords);
        // 封装数据返回结果
        return CommonData.data(pageInfo);

    }

    @Override
    public MarketKeyword createKeyword(KeywordBO keywordBO) {
        MarketKeyword marketKeyword = new MarketKeyword();
        marketKeyword.setIsHot(keywordBO.getIsHot());
        marketKeyword.setIsDefault(keywordBO.getIsDefault());
        marketKeyword.setUrl(keywordBO.getUrl());
        marketKeyword.setKeyword(keywordBO.getKeyword());
        Date date = new Date(System.currentTimeMillis());
        marketKeyword.setAddTime(date);
        marketKeyword.setUpdateTime(date);
        marketKeywordMapper.insertSelective(marketKeyword);
        return marketKeyword;
    }

    @Override
    public void deleteKeyword(MarketKeyword marketKeyword) {
        marketKeywordMapper.deleteByPrimaryKey(marketKeyword.getId());
        return;
    }

    @Override
    public MarketKeyword updateKeyword(MarketKeyword marketKeyword) {
        Date date = new Date(System.currentTimeMillis());
        marketKeyword.setUpdateTime(date);
        marketKeywordMapper.updateByPrimaryKeySelective(marketKeyword);
        return marketKeyword;
    }
}
